﻿#include "CustomMessageDialog.h"
#include "CustomButton.h"
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFrame>

CustomMessageDialog::CustomMessageDialog(QWidget* parent, PROC_TYPE nType)
    : QDialog(parent)
    , m_pTitleLabel(nullptr)
    , m_pMessageLabel(nullptr)
    , m_pToolFrame1(nullptr)
    , m_pToolFrame2(nullptr)
    , m_bBtnRetNeed(false)
    , m_nProcStatus(PROC_STATUS_UNKNOW)
    , m_pLineInput(nullptr)
{
    // 设置对话框的尺寸策略
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    setWindowFlags(Qt::CustomizeWindowHint);
    //setWindowFlag(Qt::WindowContextHelpButtonHint, false);    // 去掉问号

    setFixedSize(400, 150);

    // 初始化布局和控件
    QVBoxLayout *pmainLayout = new QVBoxLayout(this);
    pmainLayout->setContentsMargins(0, 0, 0, 0);

    QFrame* pInfoFrame = new QFrame(this);
    pmainLayout->addWidget(pInfoFrame);
    pInfoFrame->setStyleSheet("background-color: #ffffff;");

    QVBoxLayout* pInfoLayout = new QVBoxLayout(pInfoFrame);
    pInfoFrame->setLayout(pInfoLayout);

    m_pTitleLabel = new QLabel(pInfoFrame);
    pInfoLayout->addWidget(m_pTitleLabel);
    m_pTitleLabel->setFixedHeight(25);
    QFont nFontConsolas("Consolas", FONT_SIZE + 2);
    nFontConsolas.setBold(true);
    m_pTitleLabel->setFont(nFontConsolas);

    if (PROC_TYPE_MSGSHOW == nType)
    {
        m_pMessageLabel = new QLabel(pInfoFrame);
        pInfoLayout->addWidget(m_pMessageLabel);
        m_pMessageLabel->setFont(gFontConsolas);
    }
    else if (PROC_TYPE_LINESHOW == nType)
    {
        m_pLineInput = new FluLineEdit(pInfoFrame);
        pInfoLayout->addWidget(m_pLineInput);
    }

    m_pToolFrame1 = new QFrame(this);
    m_pToolFrame1->setFixedHeight(35);
    m_pToolFrame1->setStyleSheet("background-color: #f3f3f3;");
    pmainLayout->addWidget(m_pToolFrame1);
    QHBoxLayout* pTool1 = new QHBoxLayout;
    m_pToolFrame1->setLayout(pTool1);
    pTool1->setContentsMargins(10, 2, 10, 2);
    m_pToolFrame1->hide();

    FluButton* pokBtn = new FluButton(this, PBN_STYLE_GREEN);
    pokBtn->setText("确认");
    connect(pokBtn, &QPushButton::clicked, this, &CustomMessageDialog::onOkButtonClicked);
    pTool1->addStretch();
    pTool1->addWidget(pokBtn);

    m_pToolFrame2 = new QFrame(this);
    m_pToolFrame2->setFixedHeight(35);
    m_pToolFrame2->setStyleSheet("background-color: #f3f3f3;");
    pmainLayout->addWidget(m_pToolFrame2);

    QHBoxLayout* pTool2 = new QHBoxLayout;
    m_pToolFrame2->setLayout(pTool2);
    pTool2->setContentsMargins(10, 2, 10, 2);

    FluButton* pokButton = new FluButton(this, PBN_STYLE_GREEN);
    FluButton* pcancelButton = new FluButton(this, PBN_STYLE_WHITE);
    pTool2->addWidget(pokButton);
    pTool2->addWidget(pcancelButton);

    pokButton->setText("确认");
    pcancelButton->setText("取消");

    connect(pokButton, &QPushButton::clicked, this, &CustomMessageDialog::onOkButtonClicked);
    connect(pcancelButton, &QPushButton::clicked, this, &CustomMessageDialog::onCancelClicked);
}

void CustomMessageDialog::setShowRetNeed(bool bShow2Btn)
{
    m_bBtnRetNeed = bShow2Btn;
}

void CustomMessageDialog::setMessage(const QString &strTitle, const QString &strInfo, bool isWarning)
{
    m_pTitleLabel->setText(strTitle);
    if (nullptr != m_pMessageLabel)
    {
        m_pMessageLabel->setText(strInfo);
        m_pMessageLabel->setStyleSheet((true == isWarning ? "color: red;" : "color: black;"));
    }

    if (false == m_bBtnRetNeed.load())
    {
        m_pToolFrame1->show();
        m_pToolFrame2->hide();
    }
    else
    {
        m_pToolFrame1->hide();
        m_pToolFrame2->show();
    }
}

void CustomMessageDialog::setTitle(const QString& strTitle)
{
    m_pTitleLabel->setText(strTitle);
}

void CustomMessageDialog::onOkButtonClicked()
{
    m_nProcStatus = PROC_STATUS_ACCEPT;
    close();
}

void CustomMessageDialog::onCancelClicked()
{
    m_nProcStatus = PROC_STATUS_REJECT;
    close();
}

PROC_STATUS CustomMessageDialog::getStatus()
{
    return m_nProcStatus;
}